package com.jinke.uang.ui.activity.validation;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jinke.uang.R;
import com.jinke.uang.base.BaseNormalActivity;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.NoDoubleClickListener;
import com.jinke.uang.common.UserAuthRouter;
import com.jinke.uang.common.UserInfo;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.bean.AuthUserInfoData;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.model.AuthModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.bean.DictionaryItemData;
import com.jinke.uang.utils.ToastUtils;
import com.jinke.uang.view.MenuDialog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 借款金额认证
 */
@Route(name = "借款金额认证", path = RouterMap.MONEY_AUTH_PAGE)
public class MoneyAuthActivity extends BaseNormalActivity <CommonPresenter,AuthModule>{
    // 需要多少钱
    private final String DICTIONARY_TYPE_NEED_MONEY = "need_money";
    private final String NEED_MONEY = "5";
    // 借款用途
    private final String DICTIONARY_TYPE_USAGE_OF_LOAN = "usage_of_loan";
    private final String USAGE_OF_LOAN="6";


    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.etNeedMoney)
    TextView etNeedMoney;
    @BindView(R.id.rlNeedMoney)
    RelativeLayout rlNeedMoney;
    @BindView(R.id.etPurpose)
    TextView etPurpose;
    @BindView(R.id.rlPurpose)
    RelativeLayout rlPurpose;
    @BindView(R.id.tvNext)
    TextView tvNext;
    private ArrayList<DictionaryItemData> mNeedMoneyList;
    private ArrayList<DictionaryItemData> mUsageList;
    private MenuDialog mMenuDialog;
    @Autowired(name = Constant.IS_BACK)
    boolean is_back;
    private final String BORROWING_NEEDS_CODE= "borrowing_needs_code";

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        topBack.setOnClickListener(viewclick);
        //获取头部信息
        topText.setText(getStringResource(R.string.money_auth_money_title));

        rlNeedMoney.setOnClickListener(viewclick);
        rlPurpose.setOnClickListener(viewclick);
        etPurpose.setOnClickListener(viewclick);
        tvNext.setOnClickListener(viewclick);
    }

    /**
     * 点击信息
     */
    private NoDoubleClickListener viewclick = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            super.onNoDoubleClick(v);
            switch (v.getId()) {
                case R.id.topBack:
                    finish();
                    break;
                case R.id.rlNeedMoney:
                    picker_need_money();
                    break;
                case R.id.rlPurpose:
                case R.id.etPurpose:
                    picker_usage();
                    break;
                case R.id.tvNext:
                    commit();
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 提交方法
     */
    private void commit() {
        String authPageCode = UserAuthRouter.getInstance().routerMap2Code(RouterMap
                .MONEY_AUTH_PAGE);
        String mNeedMoney = (String) etNeedMoney.getTag();
        String mUsage = (String) etPurpose.getTag();
        if (TextUtils.isEmpty(mNeedMoney)) {
            ToastUtils.showShort(this, R.string.money_auth_need_money_hint);
            return;
        } else if (TextUtils.isEmpty(mUsage)) {
            ToastUtils.showShort(this, R.string.money_auth_usage_hint);
            return;
        }

        mPresenter.getData(HttpConstant.MONEY_SAVE_PERSONAL_AUTH,null,mNeedMoney, mUsage, authPageCode);
        showLoadingView();
    }

    private void picker_need_money() {
        if (mNeedMoneyList == null || mNeedMoneyList.size() == 0) {
            return;
        }
        getMenuDialog().
                showDialog(etNeedMoney, mNeedMoneyList);
    }

    private void picker_usage() {
        if (mUsageList == null || mUsageList.size() == 0) {
            return;
        }
        getMenuDialog().
                showDialog(etPurpose, mUsageList);
    }

    public MenuDialog getMenuDialog() {
        if (mMenuDialog == null) {
            mMenuDialog = new MenuDialog(this, R.style.dialog_bottom_full);
        }
        return mMenuDialog;
    }

    @Override
    protected void onUIReady() {
        ARouter.getInstance().inject(this);
        mPresenter.getData(HttpConstant.DICTIONARY_DATA,NEED_MONEY,DICTIONARY_TYPE_NEED_MONEY);
        mPresenter.getData(HttpConstant.DICTIONARY_DATA,USAGE_OF_LOAN,DICTIONARY_TYPE_USAGE_OF_LOAN);

        if (UserInfo.isShowUserInformation()) {
            mPresenter.getData(HttpConstant.MONEY_AUTH_INFO_DATA,null,BORROWING_NEEDS_CODE);
        }
        if (is_back) {
            tvNext.setText(R.string.save_change);
        }

    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_auth_money;
    }

    @Override
    protected int getResId() {
        return 0;
    }

    @Override
    public String getPageTitle() {
        return null;
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected AuthModule getModule() {
        return new AuthModule();
    }

    public void fillTextView(AuthUserInfoData.BorrowingNeedsBean borrowingNeedsBean) {
        etNeedMoney.setText(borrowingNeedsBean.getNeedMoneyValue());
        etNeedMoney.setTag(borrowingNeedsBean.getNeedMoney());
        etPurpose.setText(borrowingNeedsBean.getMoneyToDoValue());
        etPurpose.setTag(borrowingNeedsBean.getMoneyTodo());
    }

    @Override
    public void showResponse(int apiType, String listType, Object o) {
        if(apiType== HttpConstant.MONEY_AUTH_INFO_DATA){ //借款金额认证接口
            BaseResponse<AuthUserInfoData> response = (BaseResponse<AuthUserInfoData>) o;
            fillTextView(response.getData().getBorrowingNeeds());
        }
        else if(apiType==HttpConstant.MONEY_SAVE_PERSONAL_AUTH){ //提交借款需求
            dismissLoadingView();
            BaseResponse<String> response = (BaseResponse<String>) o;
            if (response.getStatus() == Constant.HTTP_OK) {
                UserAuthRouter.getInstance().jumpNextPager(this, RouterMap.MONEY_AUTH_PAGE);
            } else {
                ToastUtils.showShort(this, response.getMessage());
            }
        }
        else if(apiType==HttpConstant.DICTIONARY_DATA){ //获取字典信息
            if(listType.equals(NEED_MONEY)){
                BaseResponse<ArrayList<DictionaryItemData>> response = (BaseResponse<ArrayList<DictionaryItemData>>) o;
                mNeedMoneyList=response.getData();
            }
            else if(listType.equals(USAGE_OF_LOAN)){
                BaseResponse<ArrayList<DictionaryItemData>> response = (BaseResponse<ArrayList<DictionaryItemData>>) o;
                mUsageList=response.getData();
            }
        }
    }

    @Override
    public void showError(Throwable e) {
        dismissLoadingView();
    }
}
